import React from 'react'
import PropTypes from 'prop-types'
import { Form, Input, Modal, Select, Row, Col } from 'antd'

const FormItem = Form.Item
const Option = Select.Option
const { TextArea } = Input

const formItemLayout = {
  labelCol: {
    span: 10,
  },
  wrapperCol: {
    span: 14,
  },
}

const modal = ({
  item = {},
  onOk,
  form: {
    getFieldDecorator,
    validateFields,
    getFieldsValue,
  },
  optionData,
  ...modalProps
}) => {
  const handleOk = () => {
    validateFields((errors) => {
      if (errors) {
        return
      }
      const data = {
        ...getFieldsValue(),
        id: item.id||1,
      }
      onOk(data)
    })
  }
  const modalOpts = {
    ...modalProps,
    onOk: handleOk,
  }
  const contractTypeOption = optionData.contractTypeData.map(d => <Option key={d.code}>{d.name}</Option>)
  const countryOption = optionData.countryCodeData.map(d => <Option key={d.code}>{d.name}</Option>)
  return (
    <Modal {...modalOpts}>
      <Form layout="horizontal">
        <Row>
          <Col span={12}>
            <FormItem label="ContactType" hasFeedback {...formItemLayout}>
              {getFieldDecorator('addressType', {
                initialValue: item.addressType||'I',
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Select style={{ width: '100%' }} showSearch>{contractTypeOption}</Select>)}
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label="Address" hasFeedback {...formItemLayout}>
              {getFieldDecorator('address', {
                initialValue: item.address,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<TextArea rows={1} />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="Country" hasFeedback {...formItemLayout}>
              {getFieldDecorator('countryCode', {
                initialValue: item.countryCode||'NZ',
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Select style={{ width: '100%' }} showSearch>{countryOption}</Select>)}
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label="City" hasFeedback {...formItemLayout}>
              {getFieldDecorator('city', {
                initialValue: item.city,
                rules: [
                  {
                    required: true,
                  },
                ],
              })(<Input />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="Subburb" hasFeedback {...formItemLayout}>
              {getFieldDecorator('state', {
                initialValue: item.state,
              })(<Input />)}
            </FormItem>
          </Col>
          <Col span={12}>
            <FormItem label="PostCode" hasFeedback {...formItemLayout}>
              {getFieldDecorator('zip', {
                initialValue: item.zip,
              })(<Input />)}
            </FormItem>
          </Col>
        </Row>
        <Row>

        </Row>
        <Row>
          <Col span={24}>
            <FormItem label="Comments">
              {getFieldDecorator('comments', {
                initialValue: item.comments,
              })(<TextArea rows={4} />)}
            </FormItem>
          </Col>
        </Row>
      </Form>
    </Modal>
  )
}

modal.propTypes = {
  form: PropTypes.object.isRequired,
  type: PropTypes.string,
  item: PropTypes.object,
  onOk: PropTypes.func,
  optionData: PropTypes.object,
}

export default Form.create()(modal)
